<?php
namespace Yd\Controller;
use Common\Controller\HomebaseController;

class YdlogController extends HomebaseController{
	
	function _initialize() {
		parent::_initialize();
		//$this->model = M();
	}
	
	/**/
	function index_1(){
		$this->display();
	}

	function index(){
		$page_row_count = 20;	//每页显示数据的条数
		//初始化变量
		//echo "1";
		if (IS_POST){
			$data['start_date'] = I('post.start_date');
			$data['end_date'] = I('post.end_date');
			$data['user_id'] = sp_get_current_userid();//2007690093
			$data['select_cg'] = I('post.select_cg',0);
			session('ydlog_data',$data); //写入SESSION，让GET的时候可以读取到表单参数
			$page = 1;
			
			$ret['fyal'] = $this->fy($data,$page_row_count,$page);//输出分页链接
			$ret['table'] = $this->page($data,$page_row_count,$page);//输出数据表
			$select_cg = $this->html_select_cg($data['select_cg']);//输出选择场馆的菜单
		} elseif (IS_GET && session("?ydlog_data")){
			$data = session('ydlog_data');
			$data['user_id'] = sp_get_current_userid();//2007690093
			$page = I('get.page',0);	//点页数的时候传入选择的页数的GET数据
			$ret['fyal'] = $this->fy($data,$page_row_count,$page);//输出分页链接
			$ret['table'] = $this->page($data,$page_row_count,$page);//输出数据表
			$select_cg = $this->html_select_cg($data['select_cg']);//输出选择场馆的菜单
		} else {
			//第一次进入时，没有写入session
			$data['start_date'] = date("Y-m-d",time());
			$data['end_date'] = date("Y-m-d",time());
			$data['user_id'] = sp_get_current_userid();
			$page = 1;
		}
		//echo "2";
		$this->assign("data",$data);
		$this->assign("select_cg",$select_cg);		//输出选择场馆的菜单
		$this->assign("show_page",$ret['fyal']);
		$this->assign("show_table",$ret['table']);
		$this->display("index");
	}
	
	function fy($data,$paging,$page){
		//输出分页的第几页的链接
		//通过存储函数查询要查询记录的总数，然后分页
		//$page表示当前选择的页，则不给链接点
		//
		$m_model = new \Think\Model();
		$sqlstr = "select fun_ret_log_yd_count('{$data['start_date']}','{$data['end_date']}',{$data['user_id']},{$data['select_cg']}) as v_count";
		//echo "sqlstr:$sqlstr";
		$result = $m_model->query($sqlstr);//返回二维数组
		$v_count = $result[0]['v_count'];	//数据记录总数
		$page_count = $v_count / $paging;	//分页数量
		//生成分页按钮
		$fyal = "";
		for($i=0;$i<=$page_count;$i++){
			if($page == $i){
				$fyal = $fyal . "<span>第" . $i . "页</span>";
			}else{
				$href = "Yd/Ydlog/index?page={$i}";		//每页的链接
				$fyal = $fyal . "<a href='" . U($href) ."'>第" . $i . "页</a>";
			}
		}
		$fyal =$fyal . "<p>总记录：{$v_count}条。</p>";
		//$ret['fyal'] = $fyal;
		return $fyal;
		
	}
	
	function page($data,$paging,$page){
		//输出分页后的数据表
		$m_model = new \Think\Model();
		$sqlstr = "call proc_ret_log_yd('{$data['start_date']}','{$data['end_date']}',{$data['user_id']},{$data['select_cg']},{$paging},{$page})";
		//echo "sqlstr:$sqlstr";
		$result = $m_model->query($sqlstr);//返回二维数组
		foreach ($result as $k=>$v){
			$ret = $ret . "<tr>";
			$ret = $ret . "<td>${v['id']}</td>";
			$ret = $ret . "<td>${v['createtime']}</td>";
			$ret = $ret . "<td>${v['yfmc']}</td>";
			$ret = $ret . "<td>${v['cgmc']}</td>";
			$ret = $ret . "<td>${v['cdmc']}</td>";
			$ret = $ret . "<td>${v['cdsj']}</td>";
			$ret = $ret . "<td>${v['riqi']}</td>";
			$ret = $ret . "<td>${v['sm']}</td>";		//说明内容
			$ret = $ret . "<td>${v['ydr']}</td>";
			$ret = $ret . "<td>${v['tel']}</td>";
			$ret = $ret . "<td>${v['czr']}</td>";
			//$ret = $ret . "<td>${v['czid']}</td>";

			$ret = $ret . "</tr>";
		}
		//var_dump($result);
		return $ret;
	}

	function html_select_cg($select){
		//输出选择场馆的HTML菜单
		if(!isset($select)){$select = 0;}
		$ret="<select name='select_cg'>";
		if($select == 0){
			$ret = $ret . "<option value='0' selected>所有场馆</option>";
		}else{
			$ret = $ret . "<option value='0'>所有场馆</option>";
		}
		if($select == 13){
			$ret = $ret . "<option value='13' selected>羽毛球馆</option>";
		}else{
			$ret = $ret . "<option value='13'>羽毛球馆</option>";
		}
		if($select == 16){
			$ret = $ret . "<option value='16' selected>乒乓球馆</option>";
		}else{
			$ret = $ret . "<option value='16'>乒乓球馆</option>";
		}
		$ret = $ret . "</select>";
		//var_dump($ret);
		return $ret;
	}

}
